var $api={};
var api={};

$api.toData=function(data){
    $api.bridge.callHandler('toData', data, function(response) {})
}
api.ajax=function(data,fn){
    $.ajax({
       url:data.url,
       data:data.data.values,
       dataType:'json',
       type:'post',
       success:function(data){
           fn(data)
       },
           error:function(){
                   // alert('获取用户数据失败，请稍后再试！')
           }
    })
}
$api.ajax=function(url,data,fn){
    typeof data=="object" ? data.sign='912e386bf2cfc8aa4fa720d94614538c' : '';
    typeof data=="object" ? data.appFlag='true' : '';
    typeof data=="object" ? data.id=263 : '';
    alert(JSON.stringify(data))
    $.ajax({
       url:'http://www.cherongdao.com/index/getCarInfo',
       data:data,
       dataType:'json',
       type:'post',
       success:function(data){
            alert(1111)
            fn(data)
           },
           error:function(XMLHttpRequest, textStatus, errorThrown){
                    alert(JSON.stringify(textStatus))
                    alert('获取用户数据失败，请稍后再试！')

           }
    })
};
$api.byId=function(id){
	return document.getElementById(id);
};

$api.css=function(el, css){
    if(typeof css== 'string' && css.indexOf(':') > 0){
        el.style && (el.style.cssText += ';' + css);
        }
    };

$api.html=function(el, html){
	if(arguments.length=== 1){
		return el.innerHTML;
	}else if(arguments.length=== 2){
		el.innerHTML=html;
		return el;
	}
};



$api.attr=function(el, name, value){
	if(arguments.length== 2){
		return el.getAttribute(name);
	}else if(arguments.length== 3){
		el.setAttribute(name, value);
		return el;
	}
};
$api.domAll=function(el, selector){
	if(arguments.length=== 1 && typeof arguments[0]== 'string'){
		if(document.querySelectorAll){
			return document.querySelectorAll(arguments[0]);
		}
	}else if(arguments.length=== 2){
		if(el.querySelectorAll){
			return el.querySelectorAll(selector);
		}
	}
};
$api.hasCls=function(el, cls){
	if(el.className.indexOf(cls) > -1){
		return true;
	}else{
		return false;
	}
};
$api.addCls=function(el, cls){
	if('classList' in el){
		el.classList.add(cls);
	}else{
		var preCls=el.className;
		var newCls=preCls +' '+ cls;
		el.className=newCls;
	}
	return el;
};
$api.removeCls=function(el, cls){
	if('classList' in el){
		el.classList.remove(cls);
	}else{
		var preCls=el.className;
		var newCls=preCls.replace(cls, '');
		el.className=newCls;
	}
	return el;
};
$api.closeWin=function(){
    connectBridge.goBackPressed();
};
$api.val=function(el, val){
	if(arguments.length=== 1){
		switch(el.tagName){
			case 'SELECT':
				var value=el.options[el.selectedIndex].value;
				return value;
				break;
			case 'INPUT':
				return el.value;
				break;
			case 'TEXTAREA':
				return el.value;
				break;
		}
	}
	if(arguments.length=== 2){
		switch(el.tagName){
			case 'SELECT':
				el.options[el.selectedIndex].value=val;
				return el;
				break;
			case 'INPUT':
				el.value=val;
				return el;
				break;
			case 'TEXTAREA':
				el.value=val;
				return el;
				break;
		}
	}
	
};



